<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Config;
use think\Request;
use think\Url;

use app\admin\model\ComValidate;
use app\admin\model\ComModel;

class Cate extends Common{

	public $arr=array();

	public function _initialize(){
		if(request()->isGet()){

			$action= strtolower ( Request::instance()->action() );
			$controller=strtolower( Request::instance()->controller());

			switch ($action) {
				
				case 'catelist':
					$this->assign('title',"分类列表");
					break;
				case 'cateinfo':
					$this->assign('title',"分类信息");
					break;
				case 'modellist':
					$this->assign('title',"模型列表");
					break;
				case 'modelinfo':
					$this->assign('title',"模型信息");
					break;
				case 'pageinfo':
					$this->assign('title',"编辑页面");
					break;
			}

			$this->assign('controller',$controller);
		}
	}

	#分类分类管理#
	
	#分类列表#
	public function catelist(){
		if(request()->isGet()){
			if(request()->isajax()){

				$page=input('page');
				$limit=input('limit',30);
				$pid=input('pid');

				$map['pid']=empty($pid)?"0":$pid;

				$list=Db::name('cate_list')->where($map)->order('catepx desc')->page($page.','.$limit)->select();
				Url::root("/index.php");
				foreach ($list as $key => $v) {

					$list[$key]['modelname']=ComModel::data_value('model_list',['modelid'=>$v['modelid']],'name');
					$list[$key]['catethumb']=adminthumb($v['catethumb']);

					$list[$key]['catename']="<a href='".url('index/index/cate',array('cateid'=>$v['cateid'],'newsid'=>$v['newsid']))."' target='_blank'>".$v['catename']."</a>";

					if($v['status']=='1'){
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-green'>正常</span>";
					}else{
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-gray'>下架</span>"; 
					}
					if($v['isnav']=='1'){
						$list[$key]['isnav']="<span class='layui-badge-rim layui-bg-green'>显示</span>";
					}else{
						$list[$key]['isnav']="<span class='layui-badge-rim layui-bg-gray'>隐藏</span>"; 
					}

				}
				$count=Db::name('cate_list')->where($map)->count('cateid');

				$arr=array(
					'code'=>"0",
					'count'=>$count,
					'data'=>$list
				);
				return json($arr);


			}

			return $this->fetch();
		}
	} 

	//递归获取所有菜单
	public function getlist($list){
		if(is_array($list)){

			foreach ($list as $k => $v) {
				
				$arr=Db::name('cate_list')->where('pid',$v['cateid'])->order('catepx desc')->field('cateid,pid,catename')->select();

				$list[$k]["list"]=$this->getlist($arr);
				
			}

			return $list;

		}

		return $list;
		
	}

	#分类数据#
	public function getcatelist($list,$num="0"){
		
		if(is_array($list)){

			$num++;

			foreach ($list as $k => $v) {

				$arr=array(
					'cateid'=>$v['cateid'],
					'catename'=>str_repeat("&nbsp;&nbsp;&nbsp;",$num)."L_".$v['catename']
				);

				array_push($this->arr,$arr);

				$arr=Db::name('cate_list')->where('pid',$v['cateid'])->order('catepx desc')->field('cateid,pid,catename')->select();

				$list[$k]["list"]=self::getcatelist($arr,$num);
				
			}

			return $list;

		}

		return $list;
	}	
	#分类信息#
	public function cateinfo(){
		if(request()->isGet()){

			//整理后的 所有的菜单
			$catelist=self::getcatelistdata();

			$cateid=input('cateid');

			if($cateid){
				$info=ComModel::data_find('cate_list',['cateid'=>$cateid]);
				if(empty($info)){
					$this->error("当前信息不存在~");
				}
				$this->assign('info',$info);
			}
			
			$this->assign('modellist',ComModel::data_all('model_list','','px desc'));
			$this->assign('pages',$this->gettemplate());
			$this->assign('catelist',$catelist);
			
			return $this->fetch();

		}else{

			$data=input('post.');
			if($data['pid']==$data['cateid']){
				$this->error("父菜单不能选择为自己~");
			}
			if(isset($data['modelid'])){
				$template=ComModel::data_value('model_list',['modelid'=>$data['modelid']],'template');
				$data['template']=explode("/",$template)[0];
			}
	
			$data['status']=empty($data['status'])?"0":$data['status'];

			unset($data['file']);

			if($data['cateid']){

				$map=array(
					'cateid'=>array('neq',$data['cateid']),
					'catename'=>$data['catename']
				);
			
				/*if(Db::name('cate_list')->where($map)->count()>0){
					$this->error("当前分类已经存在!");
				}*/

				if($data['url'] ==''){
					if($data['pid'] !='0'){
						$data['url']=config('site.rukou')."/cate-{$data['cateid']}-pid-{$data['pid']}.html";
					}else{
						$data['url']=config('site.rukou')."/cate-{$data['cateid']}.html";
					}
					
				}
				ComModel::data_up('cate_list',$data,url('catelist'));

			}else{
				
				/*if(Db::name('cate_list')->where(['catename'=>$data['catename']])->count()>0){
					$this->error("当前分类已经存在!");
				}*/

				$cateid=Db::name('cate_list')->insertGetId($data);
				if($cateid){
					if($data['url'] ==''){
						if($data['pid'] !='0'){
							$url=config('site.rukou')."/cate-{$cateid}-pid-{$data['pid']}.html";
						}else{
							$url=config('site.rukou')."/cate-{$cateid}.html";
						}
						
					}
					Db::name('cate_list')->where(['cateid'=>$cateid])->update(['url'=>$url]);
					$this->success("分类添加成功~",url('catelist'));
				}else{
					$this->error("添加失败，请重试~");
				}
				

			}
		}
	}

	#删除分类#
	public function catedel(){

		$cateid=input('cateid');

		if(config('site.file_del')=='1'){
			$this->removefile(ComModel::data_value('cate_list',['cateid'=>$cateid],"catethumb"));
		}

		echo ComModel::data_del('cate_list',['cateid'=>$cateid]);
		
	}


	#所有分类数据#
	public function getcatelistdata(){
		$catelist=Db::name('cate_list')->where('pid','0')->order('catepx desc')->field('cateid,pid,catename')->select();

		self::getcatelist($catelist);
			
		return $this->arr;
	}
	#页面编辑#
	public function pageinfo(){
		if(request()->isGet()){
		
			$cateid=input('cateid');

			if(empty($cateid)){
				$this->error("当前cateid不存在");
			}

			$info=ComModel::data_find('cate_list',['cateid'=>$cateid]);
			
			$this->assign('template',$this->dir.$info['template'].'.html');

			return $this->fetch();

		}else{

			$data=input('post.');
			file_put_contents($data['template'], htmlspecialchars_decode($data['pageinfo']));
			$this->success("页面编辑成功~");
		}
	}

	#模型管理#
	public function modellist(){
		if(request()->isGet()){
			if(request()->isajax()){

				$page=input('page');
				$limit=input('limit',30);
				$pid=input('pid');

				$list=Db::name('model_list')->where($map)->order('px desc')->page($page.','.$limit)->select();
		
				$count=Db::name('model_list')->where($map)->count('modelid');

				$arr=array(
					'code'=>"0",
					'count'=>$count,
					'data'=>$list
				);
				return json($arr);
			}

			return $this->fetch();
		}
	}
	#模型信息#
	public function modelinfo(){
		if(request()->isGet()){

			$modelid=input('modelid');

			if($modelid){
				$info=ComModel::data_find('model_list',['modelid'=>$modelid]);
				if(empty($info)){
					$this->error("当前信息不存在~");
				}

				$info['template']=explode('/',$info['template']);


				$this->assign('info',$info);
			}
			
			//获取所有页面
			$this->assign('pages',$this->gettemplate());

			return $this->fetch();

		}else{

			$data=input('post.');

			$data['status']=empty($data['status'])?"0":$data['status'];

			if($data['modelid']){

				$map=array(
					'modelid'=>array('neq',$data['modelid']),
					'name'=>$data['name']
				);
			
				if(Db::name('model_list')->where($map)->count()>0){
					$this->error("当前模型已经存在!");
				}

				//修改cate 模板
				$template=ComModel::data_value('model_list',['modelid'=>$data['modelid']],'template');
				
				if($template !=$data['template']){

					if($data['type']=='列表类型'){
						$update['template']=$data['template'][1];
					}else if($data['type']=='单页类型'){
						$update['template']=$data['template'][0];
					}

					//更新分类模板
					Db::name('cate_list')->where('modelid',$data['modelid'])->update($update);

					$catelist=Db::name('cate_list')->where('modelid',$data['modelid'])->column('cateid');

					//更新新闻模板
					foreach($catelist as $v){
						//更新新闻模板
						Db::name('news_list')->where('cateid',$v)->update($update);

					}
				}

				if($data['type']=='列表类型'){
					$data['template']=$data['template'][1]."/".$data['template'][2];
				}else if($data['type']=='单页类型'){
					$data['template']=$data['template'][0];
				}

				ComModel::data_up('model_list',$data,url('modellist'));

			}else{

				$map=array(
					'name'=>$data['name']
				);
			
				if(Db::name('model_list')->where($map)->count()>0){
					$this->error("当前模型已经存在!");
				}
				$data['addtime']=date('Y-m-d H:i:s',time());
				ComModel::data_add("model_list",$data,url('modellist'));

			}
		}
	}
	#删除模型#
	public function modeldel(){

		$modelid=input('modelid');

		echo ComModel::data_del('model_list',['modelid'=>$modelid]);
	}


}